<?php
/**
 * 常见问题
 * User: ws
 * Date: 2018/11/1
 * Time: 18:25
 */

namespace app\admin\controller;
use app\admin\model\ProblemClassification as PCModeL;
use app\admin\model\Problem as PModeL;

class Problem   extends Init
{
    function index(){
        if ($this->request->isAjax()) {
            $key = input('key')?input('key'):'';

            if(is_numeric(input('c_id')) === true){
                $where['c_id'] = input('c_id');
            }
            $where['name|content'] = array('like','%'.trim($key).'%');

            $list = PModeL::where($where)->order('sort desc ,problem_id desc')->paginate(input('limit'))->toArray();

            foreach ($list['data'] as $key=>$itme){
                $list['data'][$key]['c_id'] = PCModeL::Where("id = {$itme['c_id']}")->value('name');
            }
            return json($result = ['code'=>0,'msg'=>'获取成功','data'=>$list['data'],'count'=>$list['total']]);
        }
        $list = PCModeL::order('sort desc')->column('id,name,sort');
        return view('list',['list'=>$list]);
    }

    function add(){
        if(request()->isPost()){
            $data = $this->request->post();
            if (!PModeL::create($data)) {
                return json(array('code'=>0,'msg'=>'添加失败'));
            }
            return json(array('code'=>200,'msg'=>'添加成功'));
        }
        $list = PCModeL::order('sort desc')->column('id,name,sort');
        return view('add',['list'=>$list]);
    }

    function edit(){
        if(request()->isPost()){
            $data = $this->request->post();
            if (!PModeL::update($data)) {
                return json(array('code'=>0,'msg'=>'修改失败'));
            }
            return json(array('code'=>200,'msg'=>'修改成功'));
        }

        $list = PModeL::where('problem_id', input('id'))->find()->toArray();
        $type = PCModeL::order('sort desc')->column('id,name,sort');
        return view('edit',['list'=>$list,'type'=>$type]);
    }


    function index2(){
        if ($this->request->isAjax()) {
            $key = input('key')?input('key'):'';
            $where['name'] = array('like','%'.trim($key).'%');

            $list = PCModeL::where($where)->order('sort desc ,id desc')->paginate(input('limit'))->toArray();
            return json($result = ['code'=>0,'msg'=>'获取成功','data'=>$list['data'],'count'=>$list['total']]);
        }
        return view('list2');
    }

    function add2(){
        if(request()->isPost()){
            $data = $this->request->post();
            if (!PCModeL::create($data)) {
                return json(array('code'=>0,'msg'=>'添加失败'));
            }
            return json(array('code'=>200,'msg'=>'添加成功'));
        }
        return view('add2');
    }

    function edit2(){
        if(request()->isPost()){
            $data = $this->request->post();
            if (!PCModeL::update($data)) {
                return json(array('code'=>0,'msg'=>'修改失败'));
            }
            return json(array('code'=>200,'msg'=>'修改成功'));
        }

        $list = PCModeL::where('id', input('id'))->find()->toArray();
        return view('edit2',['list'=>$list]);
    }

    function del2(){
        $id = input('param.id');
        $data = PModeL::where('c_id',$id)->find();
        if($data){
            return json(array('code'=>0,'msg'=>'删除失败，已有问题在类别下不能删除'));
        }
        if (!PCModeL::destroy(['id' => $id])) {
            return json(array('code'=>0,'msg'=>'删除失败'));
        }
        return json(array('code'=>200,'msg'=>'删除成功'));
    }

}